package com.yixin.vuehr.web.controller.system;

import cn.hutool.core.util.StrUtil;
import com.yixin.vuehr.bean.RespBean;
import com.yixin.vuehr.model.Hr;
import com.yixin.vuehr.model.Role;
import com.yixin.vuehr.service.HrService;
import com.yixin.vuehr.service.RoleService;
import com.yixin.vuehr.util.OpreateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/system/hr")
public class HrController {
    @Autowired
    HrService hrService;
    @Autowired
    RoleService roleService;
    @GetMapping("/")
    public List<Hr> getAllHrs(String keywords){
        return hrService.getAllHrs(keywords);
    }

    @PutMapping("/")
    public RespBean updateHr(@RequestBody Hr hr, Authentication authentication){
        if (hrService.updateHr(hr) == 1){
            //修改session中的hr
            SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(hr,authentication.getCredentials(),authentication.getAuthorities()));
            OpreateUtil.addOp("更新了HR信息");
            return RespBean.ok("跟新成功");
        }
        return RespBean.error("跟新失败");
    }

    @PutMapping("/password")
    public RespBean updateHrPasword(String password,String repassword){
        if (StrUtil.isNotEmpty(password)&&StrUtil.isNotEmpty(repassword)&&password.equals(repassword)){
            if (hrService.updateHrPasword(password) == 1){
                OpreateUtil.addOp("修改了密码");
                return RespBean.ok("密码跟新成功");
            }
        }
        return RespBean.error("密码跟新失败");
    }

    @GetMapping("/roles")
    public List<Role> getAllRoll(){
        return roleService.getAllRoles();
    }

    @PostMapping("/")
    public RespBean createRoll(@RequestBody Hr hr){
        if (hrService.createHr(hr) == 1){
            OpreateUtil.addOp("添加了HR:"+hr.getName());
            return RespBean.ok("添加成功");
        }
        return RespBean.error("添加失败");
    }

    @PutMapping("/role")
    public RespBean updateHrRole(Integer hrid,Integer[] rids){
        if (hrService.updateHrRole(hrid,rids)){
            OpreateUtil.addOp("跟新了HR的role,hrid="+hrid);
            return RespBean.ok("跟新成功");
        }
        return RespBean.error("跟新失败");
    }
    @DeleteMapping("/{id}")
    public RespBean deleteHrById(@PathVariable Integer id){
        if (hrService.deleteHrById(id) ==1){
            OpreateUtil.addOp("删除了HR，id="+id);
            return RespBean.ok("删除成功");
        }
        return RespBean.error("删除失败");
    }
}
